//
//  HDStatusRetweedView.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/15.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit

class HDStatusRetweedView: UIView {
//MARK -初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//MARK -添加子控件和添加约束
    private func prepareUI(){
        addSubview(bgBtn)
        addSubview(contentLabel)
        addSubview(pictureView)
        //约束
        bgBtn.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(self).offset(cellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(cellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            //由于不知图片多少   就先写零之后再更新 TODO
            make.size.equalTo(CGSizeZero)
        }
        //添加底部约束
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(cellMargin)
        }
        
    }
    
//MARK -属性观察期给子控件赋值
    var status:HDStatus?{
        didSet{
            //TODO
            let name = status?.user?.name ?? "未知用户名"
            let text = status?.text ?? "无转发内容"
            let retweetText = "@\(name):\(text)"
            contentLabel.text = retweetText
            pictureView.pictureURLs = status?.storePictureURLs //这里面就会更新高度
            //如果有配图就需要文本到图片的间距 如果没有间距就没有了
             pictureView.snp_makeConstraints { (make) -> Void in
                let margin =  status?.storePictureURLs == nil ? 0 :cellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            }


            
        }
    }
//MARK -懒加载控件
    //背景按钮
    private lazy var bgBtn:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
         button.addTarget(self, action: Selector("btnClick"), forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    @objc private func btnClick(){
        print("XXXX")
    }
    //文本
    private lazy var contentLabel:UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontsize: HDRetweedFontSize, numberOflines: 0)
        //label的宽度
        label.preferredMaxLayoutWidth = ScreenW - 2 * cellMargin
        return label
    }()
    //图片
    private lazy var  pictureView:HDPicturesView = HDPicturesView()
}
